home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / OutOfContextMenus / Source / CRebootBehavior.cp < prev    next >
Encoding:
Text File  |  1999-06-25  |  7.5 KB  |  297 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CRebootBehavior.cp                 ©1999 Eric Traut
  3. // ===========================================================================
  4.  
  5. #include "CRebootBehavior.h"
  6. #include "CShadowWindow.h"
  7.  
  8.  
  9. PixPatHandle            CRebootBehavior::sBackgroundPixPat        = NULL;
  10. PicHandle                CRebootBehavior::sMacOSPicture            = NULL;
  11. CIconHandle                CRebootBehavior::sHappyMacIcon            = NULL;
  12. PicHandle                CRebootBehavior::sProgressPicture        = NULL;
  13. PicHandle                CRebootBehavior::sSliderPicture            = NULL;
  14. IconSuiteRef            CRebootBehavior::sInitIcons[kTotalINITIcons];
  15. SndListHandle            CRebootBehavior::sBootBeepSound            = NULL;
  16. SndChannelPtr            CRebootBehavior::sSndChannel            = NULL;
  17.  
  18.  
  19. // ---------------------------------------------------------------------------
  20. //        • CRebootBehavior
  21. // ---------------------------------------------------------------------------
  22.  
  23. CRebootBehavior::CRebootBehavior(
  24.     CShadowWindow &        inShadowWindow)
  25.     :    COffscreenBehavior(inShadowWindow, false, true)
  26. {
  27.     mCurRebootState = kRebootStateBlackScreen;
  28.     mTicksStateStarted = ::TickCount();
  29.     mTicksInState = GetTicksForState(mCurRebootState);
  30. }
  31.  
  32.  
  33. // ---------------------------------------------------------------------------
  34. //        • GetTicksForState
  35. // ---------------------------------------------------------------------------
  36.  
  37. void
  38. CRebootBehavior::DoIdleTask(
  39.     Boolean     inGNETime)
  40. {
  41.     if (inGNETime && mCurRebootState == kRebootStateTearDown)
  42.     {
  43.         // Detach ourselves
  44.         mShadowWindow.DetachBehavior();
  45.     }
  46.     else
  47.     {
  48.         COffscreenBehavior::DoIdleTask(inGNETime);
  49.     }
  50. }
  51.  
  52.  
  53. // ---------------------------------------------------------------------------
  54. //        • GetTicksForState
  55. // ---------------------------------------------------------------------------
  56.  
  57. UInt32
  58. CRebootBehavior::GetTicksForState(
  59.     RebootState inState)
  60. {
  61.     switch (inState)
  62.     {
  63.         case kRebootStateBlackScreen:
  64.             return 60;
  65.             break;
  66.         
  67.         case kRebootStateBootBeep:
  68.             return 0;
  69.             break;
  70.         
  71.         case kRebootStateGrayScreen:
  72.             return 120;
  73.             break;
  74.         
  75.         case kRebootStateHappyMac:
  76.             return 200;
  77.             break;
  78.         
  79.         case kRebootStateMacOSScreen:
  80.             return 600;
  81.             break;
  82.         
  83.         case kRebootStateInit:
  84.             return 900;
  85.             break;
  86.             
  87.         case kRebootStateTearDown:
  88.             return 100000;
  89.             break;
  90.     }
  91.     
  92.     return 0;
  93. }
  94.  
  95.  
  96. // ---------------------------------------------------------------------------
  97. //        • CenterRect
  98. // ---------------------------------------------------------------------------
  99.  
  100. void
  101. CRebootBehavior::CenterRect(
  102.     const Rect &        inBounds,
  103.     Rect &                outCenterRect,
  104.     SInt16                inBoxWidth,
  105.     SInt16                inBoxHeight)
  106. {
  107.     outCenterRect.left = (inBounds.left + inBounds.right - inBoxWidth) / 2;
  108.     outCenterRect.right = outCenterRect.left + inBoxWidth;
  109.     outCenterRect.top = (inBounds.top + inBounds.bottom - inBoxHeight) / 2;
  110.     outCenterRect.bottom = outCenterRect.top + inBoxHeight;
  111. }
  112.  
  113.  
  114. // ---------------------------------------------------------------------------
  115. //        • DrawMacOSScreen
  116. // ---------------------------------------------------------------------------
  117.  
  118. void
  119. CRebootBehavior::DrawMacOSScreen(
  120.     ConstStr255Param    inMsgString,
  121.     UInt32                inTicksSinceStart,
  122.     UInt32                inINITsToDraw)
  123. {
  124.     Rect         centerRect;
  125.  
  126.     ::BackPixPat(sBackgroundPixPat);
  127.     ::EraseRect(&mGWorldRect);
  128.     ::PenNormal();
  129.     
  130.     CenterRect(mGWorldRect, centerRect, sMacOSPicture[0]->picFrame.right, sMacOSPicture[0]->picFrame.bottom);
  131.     ::DrawPicture(sMacOSPicture, ¢erRect);
  132.     
  133.     // Draw the progress bar
  134.     CenterRect(mGWorldRect, centerRect, sProgressPicture[0]->picFrame.right, sProgressPicture[0]->picFrame.bottom);
  135.     ::OffsetRect(¢erRect, 0, 90);
  136.     ::DrawPicture(sProgressPicture, ¢erRect);
  137.  
  138.     centerRect.left += 15 - 4;
  139.     centerRect.right = centerRect.left + sSliderPicture[0]->picFrame.right + 4;
  140.     centerRect.top = (centerRect.bottom + centerRect.top - sSliderPicture[0]->picFrame.bottom + 2) / 2;
  141.     centerRect.bottom = centerRect.top + sSliderPicture[0]->picFrame.bottom;
  142.  
  143.     SInt32 timesToRedraw = inTicksSinceStart / 10 + 4;
  144.     
  145.     // Mike Neil told me to comment this
  146.     while (timesToRedraw > 0)
  147.     {
  148.         if (timesToRedraw >= 4)
  149.         {
  150.             timesToRedraw -= 4;
  151.             ::OffsetRect(¢erRect, 4, 0);
  152.         }
  153.         else
  154.         {
  155.             timesToRedraw--;
  156.             ::OffsetRect(¢erRect, 1, 0);
  157.         }
  158.  
  159.         ::DrawPicture(sSliderPicture, ¢erRect);
  160.     }
  161.     
  162.     ::TextFont(systemFont);
  163.     ::TextFace(normal);
  164.     ::TextSize(0);
  165.     
  166.     UInt16 stringWidth = ::StringWidth(inMsgString);
  167.     ::MoveTo((mGWorldRect.left + mGWorldRect.right - stringWidth) / 2,
  168.                 (mGWorldRect.top + mGWorldRect.bottom + 142) / 2);
  169.     ::DrawString(inMsgString);
  170.     
  171.     centerRect = mGWorldRect;
  172.     centerRect.bottom -= 16;
  173.     centerRect.top = centerRect.bottom - 32;
  174.     centerRect.left = 20;
  175.     centerRect.right = centerRect.left + 32;
  176.     
  177.     if (inINITsToDraw > kTotalINITIcons)
  178.         inINITsToDraw = kTotalINITIcons;
  179.     
  180.     for (UInt32 iconIndex = 0; iconIndex < inINITsToDraw; iconIndex++)
  181.     {
  182.         ::PlotIconSuite(¢erRect, atNone, kTransformNone, sInitIcons[iconIndex]);
  183.         ::OffsetRect(¢erRect, 50, 0);
  184.     }
  185. }
  186.  
  187.  
  188. // ---------------------------------------------------------------------------
  189. //        • RenderToGWorld
  190. // ---------------------------------------------------------------------------
  191.  
  192. Boolean
  193. CRebootBehavior::RenderToGWorld(
  194.     StGWorldLocker &        inRenderingLocker)
  195. {
  196.     #pragma unused (inRenderingLocker)
  197.     
  198.     UInt32        curTicks = ::TickCount();
  199.     UInt32        ticksSinceStart = curTicks - mTicksStateStarted;
  200.     Rect         centerRect;
  201.     
  202.     ThrowIfNULL_(sBackgroundPixPat);
  203.     ThrowIfNULL_(sMacOSPicture);
  204.     ThrowIfNULL_(sHappyMacIcon);
  205.  
  206.     switch (mCurRebootState)
  207.     {
  208.         case kRebootStateBlackScreen:
  209.             ::BackColor(blackColor);
  210.             ::EraseRect(&mGWorldRect);
  211.             break;
  212.         
  213.         case kRebootStateBootBeep:
  214.             (void) ::SndPlay(sSndChannel, sBootBeepSound, false);
  215.             break;
  216.         
  217.         case kRebootStateGrayScreen:
  218.             ::ForeColor(blackColor);
  219.             ::BackColor(whiteColor);
  220.             ::PenPat(&qd.gray);
  221.             ::PaintRect(&mGWorldRect);
  222.             break;
  223.         
  224.         case kRebootStateHappyMac:
  225.             ::ForeColor(blackColor);
  226.             ::BackColor(whiteColor);
  227.             ::PenPat(&qd.gray);
  228.             ::PaintRect(&mGWorldRect);
  229.  
  230.             // Draw the happy mac
  231.             CenterRect(mGWorldRect, centerRect, 32, 32);
  232.             ::PlotCIcon(¢erRect, sHappyMacIcon);
  233.             break;
  234.         
  235.         case kRebootStateMacOSScreen:
  236.             DrawMacOSScreen("\pWelcome To MacOS", ticksSinceStart, 0);
  237.             break;
  238.         
  239.         case kRebootStateInit:
  240.             DrawMacOSScreen("\pStarting Up…", ticksSinceStart + GetTicksForState(kRebootStateMacOSScreen) + 200, ticksSinceStart / 100 + 1);
  241.             break;
  242.         
  243.         case kRebootStateTearDown:
  244.             
  245.             break;
  246.     }
  247.     
  248.     // Do we need to switch to the next state in 
  249.     // the state machine?
  250.     if (curTicks - mTicksStateStarted >= mTicksInState)
  251.     {
  252.         mCurRebootState++;
  253.         mTicksInState = GetTicksForState(mCurRebootState);
  254.         mTicksStateStarted = curTicks;
  255.     }
  256.  
  257.     return true;
  258. }
  259.  
  260.  
  261. // ---------------------------------------------------------------------------
  262. //        • Initialize                                        [static]
  263. // ---------------------------------------------------------------------------
  264.  
  265. void
  266. CRebootBehavior::Initialize(
  267.     SndChannelPtr        inSoundChannel)
  268. {
  269.     sBackgroundPixPat        = ::GetPixPat(128);
  270.     Assert_(sBackgroundPixPat != NULL);
  271.     
  272.     sMacOSPicture            = ::GetPicture(128);
  273.     Assert_(sMacOSPicture != NULL);
  274.     
  275.     sHappyMacIcon            = ::GetCIcon(500);
  276.     Assert_(sHappyMacIcon != NULL);
  277.  
  278.     sProgressPicture        = ::GetPicture(129);
  279.     Assert_(sProgressPicture != NULL);
  280.  
  281.     sSliderPicture            = ::GetPicture(130);
  282.     Assert_(sSliderPicture != NULL);
  283.  
  284.     for (UInt32 iconIndex = 0; iconIndex < kTotalINITIcons; iconIndex++)
  285.     {
  286.         (void) ::GetIconSuite(&sInitIcons[iconIndex], iconIndex + 128, svAllLargeData);
  287.         Assert_(sInitIcons[iconIndex] != NULL);
  288.     }
  289.  
  290.     sSndChannel             = inSoundChannel;
  291.     sBootBeepSound            = reinterpret_cast<SndListHandle>(::Get1Resource('snd ', 128));
  292.     Assert_(sBootBeepSound != NULL);
  293. }
  294.  
  295.  
  296.     
  297.